﻿package com.unifiedfield {
	import flash.display.*;
	import flash.events.*;
	import flash.utils.Timer;
	import flash.net.*;
	import com.unifiedfield.*;
	// import caurina.transitions.Tweener;

	public class Watermark extends MovieClip {

					// set this to sample filepath
		public var sampleURL = "C://matt/sony/LoginAudio/current/AudSamples.txt";
		private var strArray = new Array;
		private var envelope = new Array;
		public var multscale:Number = 0.005;
		public var fallaway = 2; //factor that the wave reduces by for each move to right
		public var maxmove = 15; //most it can change by in one move
		public var DCOffset = 0;
		public var limitPoints = 2400;
		private var numData = 0;
		private var cShape:Shape = new Shape;
		private var cHolder:MovieClip = new MovieClip;
		
		private var counter = 0;
		public var cEnvOut:Array = new Array();
		public var cEnvIn:Array = new Array();
		private var numPoints:Number = 0;  // num of points that we have

		private var ringsize = 100; //how wide the ring is
		private var watermarkSimplify:Number;  // factor by which to simplify shape
		public var spin:Number = 0;	// speed at which to rotate it

		public var userColor:Number = 0;  // the user's theme color
		public var markAlpha:Number = .5;
		public var markBlend:String = "NORMAL";
		/*private var colorArr:Array = new Array(
			[255,157,129],
			[255,255,15],
			[210,255,10],
			[146,250,255],
			[50,190,255],
			[250,96,222],
			[232,8,13],
			[255,200,0],
			[111,245,23],
			[48,199,177],
			[30,129,240],
			[140,30,202],  
			[71,0,0],
			[255,126,0],
			[38,179,0],
			[0,93,28],
			[0,48,174],
			[48,3,146]);*/

		public function Watermark(clp:MovieClip, cx:Number = 0, cy:Number = 0, clr:Number=0, alf:Number=.5, blend:String="NORMAL", ringRadius:Number = 100, waveScale:Number = 0.005, simplify:Number = 20, spinrate:Number = 0, url:String ="C://matt/sony/LoginAudio/current/AudSamples.txt") {
			userColor = clr;
			markAlpha = alf;
			markBlend = blend;
			watermarkSimplify = simplify;
			multscale = waveScale;
			spin = spinrate;
			sampleURL = url;
			numPoints = 0;  //first, 1 point and then end (first and end will be zero)
			envelope.push([0,0]);
			strArray.push("0"); //set a zero at the start
			
			clp.addChild(cHolder);
			
			cHolder.addChild(cShape);
			cHolder.x = cx;
			cHolder.y = cy;
			ringsize = ringRadius;
			function xmlLoaded(event:Event):void {
				var myXML:XML = new XML();
				myXML = XML(event.target.data);
				addSamplePoints(myXML);
			}
			var myXMLURL:URLRequest = new URLRequest(sampleURL);
			var myLoader:URLLoader = new URLLoader(myXMLURL);
			myLoader.addEventListener("complete", xmlLoaded);
		}

		private function addSamplePoints (sIn) {
			var newArray = new Array();
			newArray = sIn.split(/,/);
			for (var i=0; i<newArray.length-1; i++) {  //length-1 to chop off the last one, cuz the return ends with
						// a comma, meaning the last entry in the newArray array is not valid
				strArray.push(newArray[i]);
			}
			numData = strArray.length;
			expandWave();
		}
		
		private function expandWave() {
			while (numData>numPoints) {
				var i=numPoints;
				if (i>0) { //first point we keep at zero
					envelope.push([0,0]); //add an empty point to start					
					//store the point and scale it
					var ptNow = 1*multscale*(Number(strArray[i])+DCOffset);
					//use the point to be one part of the envelope, the second part will be
					//a decay of the last value
					if (ptNow>0) {
						if (ptNow -envelope[i-1][0]>maxmove) {
							ptNow = envelope[i-1][0]+maxmove;
						} else if (ptNow-envelope[i-1][0]<-1*maxmove) {
							ptNow = envelope[i-1][0]-maxmove;
						}
						envelope[i][0] = ptNow;
						envelope[i][1] = envelope[i-1][1]/fallaway;
					} else {
						if (ptNow -envelope[i-1][1]>maxmove) {
							ptNow = envelope[i-1][1]+maxmove;
						} else if (ptNow-envelope[i-1][1]<-1*maxmove) {
							ptNow = envelope[i-1][1]-maxmove;
						}
						envelope[i][1] = ptNow;
						envelope[i][0] = envelope[i-1][0]/fallaway;
					}
				}
				numPoints++; //move to next point
			}
			curveTail(watermarkSimplify);
		}

		public function curveTail(div:Number) {
			//trace("total data points = "+envelope.length);
			var wd = numPoints/div;
			var px:Number;
			var py:Number;
			var p2x:Number;
			var p2y:Number;
			cEnvOut = [];
			cEnvIn = [];
			for (var j=0; j<numPoints; j=j+div) {
				var pt0 = envelope[j][0];
				var pt1 = envelope[j][1];
				if (j>1 && j<numPoints-2) {
					pt0 = (envelope[j-2][0]+envelope[j-1][0]+envelope[j][0]+envelope[j+1][0]+envelope[j+2][0])/5;
					pt1 = (envelope[j-2][1]+envelope[j-1][1]+envelope[j][1]+envelope[j+1][1]+envelope[j+2][1])/5;
				}
				if (pt0>120) {
					pt0=120;
				} else if (pt0<-120) {
					pt0 = -120;
				}
				if (pt1>120) {
					pt1=120;
				} else if (pt1<-120) {
					pt1 = -120;
				}
				var currentAngle:Number = 90+((j/numPoints)*360);
				var radians = currentAngle * (Math.PI/180);
				px = (pt0-ringsize)*Math.cos(radians);
				py = (pt0-ringsize)*Math.sin(radians);
				p2x = (pt1-ringsize)*Math.cos(radians);
				p2y = (pt1-ringsize)*Math.sin(radians);
				cEnvIn.push([px,py]);
				cEnvOut.push([p2x,p2y]);
			}	
			drawCurve();
		}

		public function drawCurve() {
			var shadeOutline:Number = 1.5;  // factor by which to darken outline
			cShape.graphics.clear();
			cShape.alpha = markAlpha;
			cShape.blendMode = markBlend;
			cShape.graphics.lineStyle(2, userColor);
			cShape.graphics.beginFill(userColor, 100);
			cShape.graphics.moveTo(cEnvOut[0][0],cEnvOut[0][1]);
			for (var j=0; j<cEnvOut.length; j++) {	// draw the outer outline
				cShape.graphics.lineTo(cEnvOut[j][0],cEnvOut[j][1]);
			}
			for (j=cEnvOut.length-1; j>=0; j--) {		// draw the inner outline
				cShape.graphics.lineTo(cEnvIn[j][0],cEnvIn[j][1]);
			}
			cShape.graphics.lineTo(cEnvOut[0][0],cEnvOut[0][1]);
			cShape.graphics.endFill();

			if (spin > 0) {	// begin rotating, if needed
				var spinTimer:Timer = new Timer(Math.floor(1000/spin));
				spinTimer.addEventListener("timer", doSpin);
				spinTimer.start();
			}
		}

		public function RGBToHex(r, g, b){
		   var hex = "0x" + r.toString(16)+g.toString(16)+b.toString(16);
		   return hex;
		}
		
		public function doSpin(e:Event) {
			cShape.rotation += .5;
			//trace("R: " + cShape.rotation);
		}
		

/*		function testHex() {
			var shadeOutline:Number = 1.5;  // factor by which to darken outline color;
			var colorChange:Shape = new Shape();
			holdCircle.addChild(colorChange);
			colorChange.graphics.clear();
			colorChange.graphics.lineStyle(2, 0x000000);
			for (var i=0; i<colorArr.length; i++) {
				colorChange.graphics.lineStyle(2, RGBToHex(colorArr[i][0]/shadeOutline,colorArr[i][1]/shadeOutline,colorArr[i][2]/shadeOutline));
				colorChange.graphics.beginFill(RGBToHex(colorArr[i][0],colorArr[i][1],colorArr[i][2]), 100);
				colorChange.graphics.moveTo(i*25,0);
				colorChange.graphics.lineTo(i*25,100);
				colorChange.graphics.lineTo((i*25)+20,100);
				colorChange.graphics.lineTo((i*25)+20,0);
				colorChange.graphics.lineTo(i*25,0);
				colorChange.graphics.endFill();
			}
		}
*/
		
	}
}